
package com.viweb.storemanager.temp;

import java.awt.Color;
import java.awt.Component;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.table.DefaultTableCellRenderer;

public class VietnameseNumberCellRenderer extends DefaultTableCellRenderer {

    protected Border noFocusBorder;
    protected Border focusBorder = UIManager.getBorder("Table.focusCellHighlightBorder");
    private Color oddLine = null;
    private Color evenLine = null;
    private Color selectedLine = null;
    private Color cellFocused = Color.WHITE;
    private Locale locale = null;

    /** Creates a new instance of MoneyCellRender */
    public VietnameseNumberCellRenderer() {
        this.oddLine = new Color(240, 240, 255);
        this.evenLine = null;
        this.selectedLine = new Color(193, 210, 238);
        this.locale = Locale.GERMANY;
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {

        java.awt.Component component = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

        if (table != null) {
            if (isSelected) {
                if (hasFocus && table.isCellEditable(row, column)) {
                    this.setBackground(cellFocused);
                } else {
                    if (selectedLine != null)
                        this.setBackground(selectedLine);
                    else
                        this.setBackground(table.getSelectionBackground());
                }
            } else {
                if (row % 2 == 0) {
                    if (evenLine != null)
                        this.setBackground(evenLine);
                    else
                        this.setBackground(table.getBackground());
                } else {
                    if (oddLine != null)
                        this.setBackground(oddLine);
                    else
                        this.setBackground(table.getBackground());
                }

            }

            this.setFont(table.getFont());
            this.setEnabled(table.isEnabled());
            this.setForeground(Color.BLACK);
        }

        if (hasFocus && table.isCellEditable(row, column)) {
            this.setBorder(focusBorder);
        } else {
            this.setBorder(noFocusBorder);
        }

        // Nếu chưa truyền giá trị thì chưa làm gì cả
        if (value == null) {
            return component;
        }

        NumberFormat nf = NumberFormat.getNumberInstance(locale);
        DecimalFormat df = (DecimalFormat) nf;
        df.applyPattern("###,###");

        try {
            ((JLabel) component).setText(df.format(value) + " ");
        } catch (Exception ex) {
            ((JLabel) component).setText(value.toString());
        }

        return component;
    }
}
